<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class GroupChat extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function index(){

        return view('web.GroupChat.WebSocket');

    }
    public function WebSocket()
    {
        //定义clientFds数组 保存所有websocket连接
        $clientFds = [];

        //创建websocket服务
        $server = new swoole_websocket_server("0.0.0.0", 9501);
        //成功 触发回调函数
        $server->on('open', function (swoole_websocket_server $server, $request) use (&$clientFds) {
           //echo "server: handshake success with fd{$request->fd}\n";
           //将所有客户端连接成功后保存到数组中
           $clientFds[] = $request->fd;

        });
        //收到消息 触发回调函数
        $server->on('message', function (swoole_websocket_server $server, $frame) use (&$clientFds) {
           //echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
           //$server->push($frame->fd, "this is server");
           //当有用户发送信息，发送所有用户
           if($clientFds){

                $response = array('status'=>'0','msg'=>'failed','data'=>'');
                $data = array();
                // 获取请求参数值
                $clientFds = $request->input("clientFds");
                // 根据参数值去向表里查找对应的数据

                // 查找完毕之后，把查找到的数据赋值给response下的data字段
                $response['data'] = $clientFds;

                $response['status'] = '200';

                $response['msg'] = 'success';


            return json_encode($response);
           }

           else if(empty($clientFds))
           {

                $response = array('status'=>'0','msg'=>'failed','data'=>'');

                $data = array();
                // 获取请求参数值
                $clientFds = $request->input("clientFds");
                // 根据参数值去向表里查找对应的数据

                // 查找完毕之后，把查找到的数据赋值给response下的data字段
                $response['data'] = $clientFds;

                $response['status'] = '400';

                $response['msg'] = '!发送内容不能为空';


               return json_encode($response);
           }


           foreach ($clientFds as $fd) {

              $server->push($fd, $frame->data);

           }

        });
        //关闭连接 触发回调函数
        $server->on('close', function ($ser, $fd) use (&$clientFds) {


           //echo "client {$fd} closed\n";
           //关闭会话 销毁标识fd
           //根据value 去数组中找对应的key
           $res = array_search($fd, $clientFds, true);

           unset($clientFds[$res]);



        });
        //启动websocket服务
        $server->start();


    }

    public function chat(){

        return view('web.GroupChat.WebSocket');

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
